#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Relation Item Detabase Toolkit

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_37"
__date__    = "2014/01/12"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import traceback

# ESiPC

# Mikan
from mikan.idt import AbstractMikanItemDBToolkit
from mikan.core import MIKAN

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

class MIKAN_IDT_RELATION(MIKAN):
	pass

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class AbstractMikanRelationIDT(AbstractMikanItemDBToolkit, MIKAN_IDT_RELATION):

	def __init__(self, itemdb):
		super(AbstractMikanRelationIDT, self).__init__(itemdb)

class MikanRelationIDT(AbstractMikanRelationIDT):

	def __init__(self, itemdb):
		super(MikanRelationIDT, self).__init__(itemdb)

#	def _wheres(self, wheres=None):
#		if wheres is not None:
#			if isinstance(wheres, list) or isinstance(wheres, tuple):
#				return tuple([wheres])
#			elif isinstance(wheres, tuple):
#				return wheres
#			else:
#				return (wheres)
#		return None

	def get_relations(self, entity, relactionclass, fromattribute, wheres=None, offset=None, limit=None):
#		w = self._wheres(wheres)
#		if w is None:
#			w = (fromattribute == entity.id)
#		else:
#			w = w + (fromattribute == entity.id)

		# where
		w = {}
		if wheres:
			w.update(wheres)

		w[fromattribute] = entity.id

		# log
		if self.islogdebug:
			self.logger.debug(w)

		return self.itemdb.gets(relactionclass, wheres=self.itemdb.where(relactionclass, relactionclass(), w), offset=offset, limit=limit)

	def get_child_relations(self, entity, relactionclass, wheres=None, offset=None, limit=None):
		return self.get_relations(entity, relactionclass, "childid", wheres, offset=offset, limit=limit)

	def get_parent_relations(self, entity, relactionclass, wheres=None, offset=None, limit=None):
		return self.get_relations(entity, relactionclass, "parentid", wheres, offset=offset, limit=limit)

	def get_friend_relations(self, entity, relactionclass, wheres=None, offset=None, limit=None):
		return self.get_relations(entity, relactionclass, "friendid", wheres, offset=offset, limit=limit)

	def extends(self, entity, relactionclass, toclass, fromattribute, toattribute, wheres=None, offset=None, limit=None):
		rs = self.get_relations(entity, relactionclass, fromattribute, wheres, offset=offset, limit=limit)

		l = []

		if rs:
			for r in rs:
				a = self.itemdb.get(toclass, r[toattribute])
				if a:
					l.append(a)

		return l

	def extend_childs(self, entity, relactionclass, toclass, wheres=None, offset=None, limit=None):
		return self.extends(entity, relactionclass, toclass, "parentid", "childid", wheres, offset=offset, limit=limit)

	def extend_parents(self, entity, relactionclass, toclass, wheres=None, offset=None, limit=None):
		return self.extends(entity, relactionclass, toclass, "childid", "parentid", wheres, offset=offset, limit=limit)

	def extend_friends(self, entity, relactionclass, toclass, wheres=None, offset=None, limit=None):
		return self.extends(entity, relactionclass, toclass, "parentid", "friendid", wheres, offset=offset, limit=limit)

	def create_relation(self, entity, relactionclass, toentity, fromattribute, toattribute, attributes=None):
		rc = relactionclass(attributes)
		rc[fromattribute] = entity.id
		rc[toattribute] = toentity.id

		return self.itemdb.create(rc)

	def create_child_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.create_relation(entity, relactionclass, toentity, "parentid", "childid", attributes)

	def create_parent_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.create_relation(entity, relactionclass, toentity, "childid", "parentid", attributes)

	def create_friend_relation(self, entity, relactionclass, toentity, attributes=None):
		return self.create_relation(entity, relactionclass, toentity, "parentid", "friendid", attributes)


# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
